/*     */ package com.zimbra.cs.util;
/*     */ 
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.L10nUtil;
/*     */ import com.zimbra.common.util.L10nUtil.MatchingPropertiesFilter;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.SetUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import java.io.File;
/*     */ import java.util.Arrays;
/*     */ import java.util.Comparator;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.Locale;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class WebClientL10nUtil
/*     */ {
/*     */   private static final String LOAD_LOCALES_ON_UI_NODE = "/fromservice/loadlocales";
/*     */   
/*     */   public static Locale[] getAllLocalesSorted()
/*     */   {
/*  44 */     Locale[] locales = Locale.getAvailableLocales();
/*  45 */     Arrays.sort(locales, new LocaleComparatorByDisplayName(Locale.US));
/*  46 */     return locales;
/*     */   }
/*     */   
/*     */   private static class LocaleComparatorByDisplayName implements Comparator<Locale> {
/*     */     private final Locale mInLocale;
/*     */     
/*     */     LocaleComparatorByDisplayName(Locale inLocale) {
/*  53 */       this.mInLocale = inLocale;
/*     */     }
/*     */     
/*     */     public int compare(Locale a, Locale b)
/*     */     {
/*  58 */       String da = a.getDisplayName(this.mInLocale);
/*  59 */       String db = b.getDisplayName(this.mInLocale);
/*  60 */       return da.compareTo(db);
/*     */     }
/*     */   }
/*     */   
/*     */   public static synchronized Set<Locale> getAvailableLocales() throws ServiceException {
/*  65 */     if (locales == null) {
/*  66 */       loadBundles();
/*     */     }
/*  68 */     return locales;
/*     */   }
/*     */   
/*     */   static enum ClientResource
/*     */   {
/*  73 */     AjxMsg, 
/*  74 */     ZMsg, 
/*  75 */     ZaMsg, 
/*  76 */     ZhMsg, 
/*  77 */     ZmMsg;
/*     */     
/*     */     private ClientResource() {} }
/*     */   
/*  81 */   static Set<Locale> locales = null;
/*     */   
/*     */ 
/*     */ 
/*  85 */   static Map<Locale, Locale[]> sortedLocalesMap = null;
/*     */   
/*     */   public static void loadBundlesByDiskScan() {
/*  88 */     String msgsDir = LC.localized_client_msgs_directory.value();
/*  89 */     ZimbraLog.misc.info("Scanning installed locales from %s", new Object[] { msgsDir });
/*  90 */     locales = new HashSet();
/*     */     
/*     */ 
/*  93 */     ZimbraLog.misc.info("Adding locale " + Locale.US.toString() + " (always added)");
/*  94 */     locales.add(Locale.US);
/*  95 */     File dir = new File(msgsDir);
/*  96 */     if (!dir.exists()) {
/*  97 */       ZimbraLog.misc.info("message directory does not exist: %s", new Object[] { msgsDir });
/*  98 */       return;
/*     */     }
/* 100 */     if (!dir.isDirectory()) {
/* 101 */       ZimbraLog.misc.info("message directory is not a directory: %s", new Object[] { msgsDir });
/* 102 */       return;
/*     */     }
/*     */     
/* 105 */     for (File file : dir.listFiles(new L10nUtil.MatchingPropertiesFilter(ClientResource.values()))) {
/* 106 */       ZimbraLog.misc.debug("loadBundlesByDiskScan processing file: %s", new Object[] { file.getName() });
/* 107 */       Locale locale = L10nUtil.getLocaleForPropertiesFile(file, true);
/* 108 */       if ((locale != null) && (!locales.contains(locale))) {
/* 109 */         ZimbraLog.misc.info("Adding locale: %s", new Object[] { locale });
/* 110 */         locales.add(locale);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private static void loadBundles() throws ServiceException {
/* 116 */     ZimbraLog.webclient.debug("Loading locales...");
/* 117 */     locales = new HashSet();
/*     */     
/* 119 */     if (WebClientServiceUtil.isServerInSplitMode()) {
/* 120 */       String localesStr = WebClientServiceUtil.sendServiceRequestToOneRandomUiNode("/fromservice/loadlocales");
/* 121 */       for (String str : localesStr.split(",")) {
/* 122 */         String[] parts = str.split("_");
/* 123 */         switch (parts.length) {
/*     */         case 1: 
/* 125 */           locales.add(new Locale(parts[0]));
/* 126 */           break;
/*     */         case 2: 
/* 128 */           locales.add(new Locale(parts[0], parts[1]));
/* 129 */           break;
/*     */         case 3: 
/* 131 */           locales.add(new Locale(parts[0], parts[1], parts[2]));
/* 132 */           break;
/*     */         default: 
/* 134 */           ZimbraLog.misc.warn("unsupported locale %s", new Object[] { str });
/*     */         }
/*     */       }
/*     */     } else {
/* 138 */       loadBundlesByDiskScan();
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 160 */     Set<Locale> pseudoLocales = new HashSet();
/* 161 */     for (Locale lc : locales) {
/* 162 */       String language = lc.getLanguage();
/* 163 */       Locale lcLang = new Locale(language);
/* 164 */       if ((!locales.contains(lcLang)) && (!pseudoLocales.contains(lcLang))) {
/* 165 */         ZimbraLog.misc.info("Adding locale " + lcLang.toString() + " (pseudo)");
/* 166 */         pseudoLocales.add(lcLang);
/*     */       }
/*     */     }
/* 169 */     if (pseudoLocales.size() > 0) {
/* 170 */       locales = SetUtil.union(locales, pseudoLocales);
/*     */     }
/* 172 */     ZimbraLog.webclient.debug("Locale loading complete.");
/*     */   }
/*     */   
/*     */   public static synchronized Locale[] getLocales(Locale inLocale) throws ServiceException {
/* 176 */     if (locales == null) {
/* 177 */       loadBundles();
/*     */     }
/*     */     
/* 180 */     Locale[] sortedLocales = null;
/* 181 */     if (sortedLocalesMap == null) {
/* 182 */       sortedLocalesMap = new HashMap();
/*     */     } else {
/* 184 */       sortedLocales = (Locale[])sortedLocalesMap.get(inLocale);
/*     */     }
/*     */     
/* 187 */     if (sortedLocales == null)
/*     */     {
/* 189 */       sortedLocales = (Locale[])locales.toArray(new Locale[locales.size()]);
/* 190 */       Arrays.sort(sortedLocales, new LocaleComparatorByDisplayName(inLocale));
/* 191 */       sortedLocalesMap.put(inLocale, sortedLocales);
/*     */     }
/* 193 */     return sortedLocales;
/*     */   }
/*     */   
/*     */   public static synchronized void flushCache() throws ServiceException {
/* 197 */     ZimbraLog.misc.debug("WebClientL10nUtil: flushing locale cache");
/* 198 */     locales = null;
/* 199 */     sortedLocalesMap = null;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/util/WebClientL10nUtil.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */